<?php

/*
 * This class is used for common same function.
 */

class Model_Invoice extends Model_Base {
	
	protected $_name,$_primaryKey;
    
    public function init() {
        $this->_name = $this->INVOICE_TBL;
        $this->_primaryKey = 'inv_id';
    }
	
	 
	public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
	public function getContractorTypeArray() {
        return array(
            "" => "-- Select Contractor Type --",
            "company" => "Company",
            "contact" => "Client"
        );
    }
	
	public function getInvoicemainStatusArray() {
        return array(
            "" => "",
            "draft" => "Draft",
            "sent_to_customer" => "Sent to customer",
			"confirmed" => "Confirmed",
            "completed" => "Completed",
			"refused" => "Refused"
        );
    }
	
	public function getPaymentmethodArray() {
        return array(
            "" => "-- Select Payment Method --",
            "bank_transfer: companies" => "Bank transfer: Companies",
            "quote (companies)" => "Quote (companies)"
        );
    }
	
	
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('inv' => $this->INVOICE_TBL))
                ->where('inv_id = ?', $id)
				->where("inv_status != 2");
     
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	 public function fetchInvoiceData($where = "", $limit = "", $offset = "0", $order = "inv_id", $sort = "ASC") {
       $select = $this->select()
                ->from(array("inv" => $this->INVOICE_TBL))
				->setIntegrityCheck(false)
				->join(array('lgn' => 'tbl_login'), "inv.inv_responsible = lgn.lgn_id")
				->where("inv_status != 2");
        if ($where != "") {
            $select->where($where);
        }
		//echo $select;exit;
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function getInvoiceCount($where = "") {
        $select = $this->select()
                ->from(array("inv" => $this->INVOICE_TBL), Array('cnt' => 'count(inv_id)'))
			    ->where("inv_status != 2");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
	
	public function deleteInvoice($invoiceArray) {
        if (is_array($invoiceArray)) {
            foreach ($invoiceArray as $_inv) {
                $invoiceData = Array();
                $invoiceData = $this->fetchEntryById($_inv);
                if (count($invoiceData) > 0) {
                    $data = array("inv_status" => "2");
                    $this->update($data, "inv_id =" . (int) $_inv);
                }
            }
        } else {
            $inv_id = $invoiceArray;
            $invoiceData = $this->fetchEntryById($inv_id);
            if (count($invoiceData) > 0) {
                $data = array("inv_status" => "2");
                $this->update($data, "inv_id =" . $inv_id);
            }
        }
    }
}